Skip to content

Feat template#9

Merged
AptS-1547 merged 10 commits intomasterfrom
feat-template
May 5, 2025
Merged

Feat template#9
AptS-1547 merged 10 commits intomasterfrom
feat-template

Conversation

@AptS-1547
Copy link
Copy Markdown
Owner

No description provided.

AptS-1547 added 3 commits May 6, 2025 00:19
… templates

- Enhanced logging in `app/models/config.py` for better error handling and configuration validation.
- Introduced `config.example.yaml` as a sample configuration file.
- Implemented application lifecycle management in `main.py` to handle OneBot client initialization and cleanup.
- Added message formatting functions for GitHub events in `main.py`.
- Created message templates for GitHub events (push, issues, pull requests) in the `templates` directory.
- Updated tests in `tests/test_onebot_sender.py` to cover new message formatting and OneBot client functionalities.
- Added dependencies for `jinja2` and `markupsafe` in `pyproject.toml` and `poetry.lock`.
@AptS-1547 AptS-1547 requested a review from Copilot May 5, 2025 16:54
@AptS-1547 AptS-1547 self-assigned this May 5, 2025
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @AptS-1547, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request introduces a new feature to handle GitHub webhooks and forward them to OneBot targets. It includes changes to the Dockerfile, adds new API endpoints for handling GitHub webhooks, creates core modules for webhook processing, modifies the configuration model, and implements OneBot client functionalities. The main goal is to enable real-time notifications of GitHub events to be sent to specified OneBot clients.

Highlights

  • GitHub Webhook Handling: Implements the core logic for receiving, verifying, and processing GitHub webhook events, specifically push events.
  • OneBot Integration: Adds functionality to send notifications to OneBot clients (both WebSocket and HTTP) upon receiving relevant GitHub webhook events.
  • Configuration Management: Updates the configuration model to include webhook settings and OneBot target configurations, with improved error handling and default configuration creation.
  • WebSocket Connection Management: Introduces a WebSocket manager with reconnection capabilities for reliable communication with OneBot instances.
  • Application Lifespan Management: Modifies the main application entrypoint to initialize and shutdown the OneBot client during the application's lifecycle.

Changelog

Click here to see the changelog
  • Dockerfile
    • Changes the VOLUME instruction from /app/config to /app/templates.
  • app/api/init.py
    • Adds a new API module for handling GitHub webhooks, including routing and tagging.
  • app/api/github_webhook.py
    • Implements the /github-webhook endpoint to process GitHub webhook requests, verify signatures, match webhook configurations, and send notifications to OneBot targets for push events.
  • app/core/init.py
    • Adds a core module for handling GitHub webhook events, including signature verification, webhook matching, and push event data extraction.
  • app/core/github.py
    • Introduces the GitHubWebhookHandler class with static methods for verifying webhook signatures, finding matching webhook configurations, and extracting push event data.
  • app/core/onebot.py
    • This file was removed.
  • app/models/init.py
    • Updates the import from Config to get_settings.
  • app/models/config.py
    • Adds logging, introduces OnebotTarget model, and implements configuration loading from YAML with validation and default configuration creation. Includes a cached get_settings function.
    • Adds lru_cache to get_settings function.
  • app/onebot/init.py
    • Adds a new module for handling OneBot client functionalities, including WebSocket and HTTP clients, message formatting, and client initialization/shutdown.
  • app/onebot/onebot.py
    • Implements OneBot V11 clients (WebSocket and HTTP) with WebSocket connection management, message sending, and utility functions for message formatting. Includes reconnection logic for WebSocket client.
  • main.py
    • Modifies the main application entrypoint to initialize and shutdown the OneBot client during the application's lifecycle using a lifespan context manager. Removes the direct webhook handling logic and integrates the API router.
    • Removes the direct webhook handling logic and integrates the API router.
  • poetry.lock
    • Updates dependencies, including adding jinja2 and markupsafe.
  • pyproject.toml
    • Adds jinja2 as a dependency.
  • tests/test_onebot_sender.py
    • Rewrites the tests to test the new OneBot client implementation, including WebSocket connection management, message sending, and message formatting.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


A webhook's call, a bot's reply,
Across the network, messages fly.
From GitHub's push to chat's delight,
Code's evolution, brought to light.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request introduces a new feature to handle GitHub webhook events and forward them to OneBot targets. The code includes several new modules and updates to existing ones, focusing on configuration, API endpoints, core logic, and OneBot client implementation. Overall, the changes seem well-structured and follow a modular approach. However, there are some areas that could be improved for better maintainability, error handling, and code clarity.

Summary of Findings

  • Missing Error Handling in OneBotWebSocketManager._message_receiver: The OneBotWebSocketManager._message_receiver method should handle asyncio.CancelledError and other exceptions more gracefully to ensure proper cleanup and reconnection attempts.
  • Inconsistent Naming Conventions: There are some inconsistencies in naming conventions, such as using camelCase for some variables and snake_case for others. Adhering to a consistent naming convention improves code readability and maintainability.
  • Potential Resource Leak in OneBotWebSocketManager: The OneBotWebSocketManager class might have a resource leak if the WebSocket connection is not properly closed in all error scenarios. Ensure that the session and ws objects are closed in the stop method and in exception handlers.

Merge Readiness

The pull request is not yet ready for merging. There are several issues that need to be addressed before merging, particularly those related to error handling, resource management, and code clarity. Addressing the critical and high severity comments is essential to ensure the stability and reliability of the new feature. I am unable to approve this pull request, and recommend that others review and approve this code before merging.

Comment on lines +179 to +181
except Exception as e: # pylint: disable=broad-except
logger.error("处理 WebSocket 消息时出错: %s", e)
break
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The broad exception handling here could mask important issues. Consider catching specific exceptions and handling them appropriately. Also, ensure that the WebSocket connection is properly closed in case of an error.

Suggested change
except Exception as e: # pylint: disable=broad-except
logger.error("处理 WebSocket 消息时出错: %s", e)
break
except asyncio.CancelledError:
logger.info("WebSocket 接收任务被取消")
except aiohttp.ClientError as e:
logger.error("处理 WebSocket 消息时出错: %s", str(e))
if self.ws:
await self.ws.close()
break
except Exception as e: # pylint: disable=broad-except
logger.error("处理 WebSocket 消息时出错: %s", str(e))
break

Comment on lines +247 to +250
except Exception as e:
self.response_futures.pop(echo_id, None)
logger.error("发送请求失败: %s", e)
raise
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The broad exception handling here could mask important issues. Consider catching specific exceptions and handling them appropriately. Also, ensure that the WebSocket connection is properly closed in case of an error.

Suggested change
except Exception as e:
self.response_futures.pop(echo_id, None)
logger.error("发送请求失败: %s", e)
raise
except asyncio.TimeoutError:
self.response_futures.pop(echo_id, None)
logger.error("请求超时:%s", echo_id)
return {
"status": "error",
"retcode": -1,
"message": "请求超时",
"echo": echo_id
}
except aiohttp.ClientError as e:
self.response_futures.pop(echo_id, None)
logger.error("发送请求失败: %s", str(e))
raise
except Exception as e:
self.response_futures.pop(echo_id, None)
logger.error("发送请求失败: %s", str(e))
raise

Comment on lines 92 to 93
setattr(config, key, value)
else:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider logging the specific exception that occurred during configuration validation for better debugging.

Suggested change
logger.warning("配置验证失败: %s", str(e))
logger.warning("使用默认配置")
logger.warning("配置验证失败: %s, Exception: %s", str(e), type(e).__name__)
logger.warning("使用默认配置")

Comment on lines +187 to +189
if self.running:
logger.info("WebSocket 连接中断,正在重新连接...")
asyncio.create_task(self._attempt_reconnect())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It's good to log the reconnection attempt, but ensure that the reconnection logic doesn't lead to infinite loops in case of persistent failures. Consider adding a maximum number of reconnection attempts or an increasing delay between attempts.

Suggested change
if self.running:
logger.info("WebSocket 连接中断,正在重新连接...")
asyncio.create_task(self._attempt_reconnect())
if self.running:
logger.info("WebSocket 连接中断,正在重新连接...")
asyncio.create_task(self._attempt_reconnect(max_retries=3))

main.py Outdated
Comment on lines +52 to +54
except Exception as e: # pylint: disable=broad-except
logger.error("初始化 OneBot 客户端失败: %s", e)
sys.exit(1)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Exiting the application with sys.exit(1) abruptly terminates the program, which might not be desirable in all cases. Consider a more graceful shutdown, such as setting a flag to prevent further processing and logging the error for monitoring purposes. Also, consider whether you want to re-raise the exception, or just log it.

Suggested change
except Exception as e: # pylint: disable=broad-except
logger.error("初始化 OneBot 客户端失败: %s", e)
sys.exit(1)
except Exception as e: # pylint: disable=broad-except
logger.error("初始化 OneBot 客户端失败: %s", e)
#sys.exit(1)
raise

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new feature template that refactors the GitHub webhook processing and OneBot client integration while updating tests and configuration management. Key changes include:

  • Updated test cases for OneBot clients and message formatting with an expanded set of fixtures and mocks.
  • Refactored the main application entry point to use a lifespan context manager for initializing and shutting down resources, along with updated API routing.
  • Updated configuration and dependency files, including adding a new dependency on jinja2.

Reviewed Changes

Copilot reviewed 18 out of 19 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/test_onebot_sender.py Refactored tests for OneBot WebSocket/HTTP clients and updated mocking details.
pyproject.toml Added a dependency on jinja2.
main.py Reworked application entry point to use lifespan context and API router.
app/onebot/init.py Introduced OneBot client exports and initialization functions.
app/models/config.py Enhanced configuration loading with logging and caching via lru_cache.
app/core/github.py Updated GitHub webhook handler with static methods.
app/core/init.py Added core module for GitHub webhook matching and validation.
app/api/github_webhook.py Created new API route for GitHub webhook processing.
app/api/init.py Included the GitHub webhook router in the main API router.
Files not reviewed (1)
  • Dockerfile: Language not supported

main.py Outdated
push_data = extract_push_data(payload)
except Exception as e: # pylint: disable=broad-except
logger.error("初始化 OneBot 客户端失败: %s", e)
sys.exit(1)
Copy link

Copilot AI May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using sys.exit(1) within the lifespan context may abruptly terminate the application; consider raising an exception to allow for a more graceful shutdown handling.

Suggested change
sys.exit(1)
raise InitializationError("OneBot client initialization failed") from e

Copilot uses AI. Check for mistakes.
# 调用发送方法,应该捕获异常并记录
await sender.send_to_onebot(message_data)
@pytest.mark.asyncio
@patch('app.core.onebot.OneBotWebSocketManager.send_request')
Copy link

Copilot AI May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The patch target for send_request refers to 'app.core.onebot', which has been removed in this PR; update the patch path to match the new module structure if needed.

Suggested change
@patch('app.core.onebot.OneBotWebSocketManager.send_request')
@patch('app.onebot.onebot.OneBotWebSocketManager.send_request')

Copilot uses AI. Check for mistakes.
@AptS-1547 AptS-1547 requested a review from Copilot May 5, 2025 17:02
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new feature template that refactors the OneBot and GitHub webhook logic, expands test coverage with additional unit tests, and updates dependencies and configuration management.

  • Refactors application startup via an async lifespan context manager and integrates API router in main.py.
  • Enhances test modules for OneBot WebSocket and HTTP clients while removing the legacy OneBotSender.
  • Updates dependency declarations (adding jinja2), configuration caching in app/models/config.py, and introduces a custom exception module.

Reviewed Changes

Copilot reviewed 20 out of 21 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/test_onebot_sender.py Adds extensive tests for OneBot client behaviors and singleton usage.
pyproject.toml Updates dependencies by adding jinja2.
main.py Refactors application lifecycle handling and API routing.
app/utils/exceptions.py Introduces the InitializationError exception.
app/models/config.py Updates configuration loading with caching and improved logging.
app/core/github.py Adds GitHubWebhookHandler static methods for request handling.
app/api/github_webhook.py Implements a new GitHub webhook routing and message formatting API.
app/api/init.py Registers the GitHub webhook router under a main API router.
Files not reviewed (1)
  • Dockerfile: Language not supported

@AptS-1547
Copy link
Copy Markdown
Owner Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request introduces a new feature to handle GitHub webhook events and forward them to OneBot targets. The code includes several new modules and updates to existing ones. Overall, the changes seem well-structured and follow a logical flow. However, there are some areas that could be improved for better maintainability, error handling, and code clarity.

Summary of Findings

  • Error Handling: The application uses broad exception handling in several places. It's recommended to catch specific exceptions to handle errors more gracefully and avoid masking unexpected issues. For example, in app/models/config.py, the except Exception as e: block should be narrowed down to specific exceptions like yaml.YAMLError or pydantic.ValidationError.
  • WebSocket Manager Reconnection Logic: The WebSocket manager's reconnection logic in app/onebot/onebot.py could be improved. Currently, the reconnection attempts are made in a loop with a delay. Consider adding a mechanism to stop reconnection attempts after a certain number of failures or if a critical error is encountered.
  • Configuration Validation: The configuration validation in app/models/config.py could be more robust. Instead of just logging a warning and using default configurations when validation fails, consider raising an exception to prevent the application from starting with invalid configurations.
  • Logging: The logging messages could be more informative. Include relevant context in the log messages to aid in debugging. For example, log the specific configuration parameter that failed validation.

Merge Readiness

The pull request is not quite ready for merging. There are several medium and high severity issues that need to be addressed. Specifically, the error handling and configuration validation need to be improved. I am unable to approve this pull request, and recommend that it not be merged until the issues are addressed and the code is reviewed and approved by others.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request introduces a new feature to handle GitHub webhook events and forward them to OneBot targets. The code includes several new modules and updates to existing ones, focusing on configuration, core logic, API endpoints, and OneBot client implementation. Overall, the changes seem well-structured, but there are some areas that could be improved for clarity, error handling, and maintainability.

Summary of Findings

  • Volume mount in Dockerfile: The Dockerfile's volume mount point was changed from /app/config to /app/templates. Ensure this change aligns with the application's intended behavior and configuration strategy.
  • WebSocket Manager Reconnection Logic: The WebSocket manager's reconnection logic could potentially lead to issues if the connection drops frequently. Consider adding a mechanism to prevent excessive reconnection attempts in a short period.
  • Exception Handling in Configuration Loading: The configuration loading mechanism in app/models/config.py could benefit from more specific exception handling to provide better diagnostics when configuration validation fails.

Merge Readiness

The pull request introduces significant functionality and requires careful review. While the code appears to be well-structured, the identified issues related to Dockerfile configuration, WebSocket reconnection logic, and exception handling should be addressed before merging. I am unable to approve this pull request, and recommend that it not be merged until the aforementioned issues are addressed (at a minimum).

@AptS-1547
Copy link
Copy Markdown
Owner Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request introduces a new feature to handle GitHub webhook events and forward them to OneBot targets. The code includes several new modules and updates to existing ones, focusing on configuration, core logic, API endpoints, and OneBot client implementation. Overall, the changes seem well-structured, but there are some areas that could be improved for clarity, error handling, and maintainability.

Summary of Findings

  • Volume Mount Point: The Dockerfile's volume mount point was changed from /app/config to /app/templates. Ensure this change aligns with the application's intended configuration and data storage strategy. Verify that the application correctly utilizes the /app/templates directory for its intended purpose.
  • WebSocket Client Reconnection Logic: The WebSocket client reconnection logic in app/onebot/onebot.py could benefit from more robust error handling and logging. Ensure that reconnection attempts are properly tracked and that failures are logged with sufficient detail to diagnose connection issues.
  • Configuration Validation and Default Handling: The configuration validation and default handling in app/models/config.py could be improved to provide more informative error messages and ensure that the application gracefully handles invalid or missing configuration values.

Merge Readiness

The pull request introduces significant new functionality and refactors existing code. While the changes appear to be well-structured, there are several areas that require further attention before merging. Specifically, the Dockerfile volume mount point, WebSocket client reconnection logic, and configuration validation and default handling should be carefully reviewed and addressed. I am unable to approve this pull request, and recommend that it not be merged until these issues are resolved and thoroughly tested.

@AptS-1547
Copy link
Copy Markdown
Owner Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request introduces a new feature to handle GitHub webhook events and forward them to OneBot targets. The code includes several new modules and updates to existing ones to implement this functionality. Overall, the code is well-structured and follows a clear architecture. However, there are some areas that could be improved for better maintainability, error handling, and code clarity.

Summary of Findings

  • Error Handling in Configuration Loading: The configuration loading mechanism in app/models/config.py could be improved to provide more informative error messages and ensure that the application fails gracefully if the configuration file is invalid or missing. Currently, the application logs a warning and proceeds with default configurations, which might lead to unexpected behavior.
  • WebSocket Client Reconnection Logic: The WebSocket client reconnection logic in app/onebot/onebot.py could be enhanced to handle various disconnection scenarios more robustly. The current implementation attempts to reconnect upon connection interruption, but it might not cover all possible failure cases, such as network errors or server-side issues.
  • Inconsistent Return Types: The send_message methods in OneBotWebSocketClient and OneBotHTTPClient in app/onebot/onebot.py have inconsistent return types. In the case of invalid message types, they return a dictionary with an error status, while in other cases, they raise exceptions. This inconsistency can make it harder to handle errors uniformly.

Merge Readiness

The pull request is not yet ready for merging. There are several issues that need to be addressed before merging, including error handling, reconnection logic, and inconsistent return types. Addressing these issues will improve the reliability and maintainability of the code. I am unable to approve this pull request, and recommend that others review and approve this code before merging.

AptS-1547 and others added 4 commits May 6, 2025 01:21
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@AptS-1547
Copy link
Copy Markdown
Owner Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request introduces a new feature to handle GitHub webhook events and forward them to OneBot targets. The code includes several new modules and updates to existing ones. Overall, the code is well-structured and follows a modular approach. However, there are some areas that could be improved for better maintainability, error handling, and code clarity.

Summary of Findings

  • Missing Error Handling in Configuration Loading: The application should gracefully handle configuration loading failures, providing informative error messages and potentially falling back to default configurations or exiting gracefully. Currently, exceptions during YAML loading or Pydantic validation are re-raised, which might not be ideal for production environments.
  • Inconsistent Logging: While logging is used throughout the application, the level of detail and consistency could be improved. Consider using more structured logging and ensuring that all important events are logged with appropriate severity levels.
  • Lack of Input Validation: The application currently trusts that the incoming webhook data is valid. Consider adding input validation to ensure that the data conforms to the expected schema and to prevent potential security vulnerabilities.

Merge Readiness

The pull request introduces significant functionality and refactoring. While the code appears functional, the identified issues regarding error handling and input validation should be addressed before merging. I am unable to approve this pull request, and recommend that it not be merged until the identified issues are resolved and that others review and approve this code before merging.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@AptS-1547 AptS-1547 merged commit d274eb6 into master May 5, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants